﻿using Entity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class DatabaseHelper
    {


        public static void InitDB()
        {
            using (DecorationDbContext context = new DecorationDbContext())
            {
                context.Database.EnsureDeleted();

                context.Database.EnsureCreated();

                #region 初始化用户相关信息
                var admin = new UserInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    Account = "admin",
                    UserName = "管理员",
                    Sex = 2,
                    PassWord = MD5Helper.MD5Encrypt64("123456"),
                    PhoneNum = "88888888888",
                    Email = "88888888888@qq.com",
                    CreateTime = DateTime.Now,
                    IsAdmin = true
                };

                var liubei = new UserInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    Account = "liubei",
                    UserName = "刘备",
                    Sex = 1,
                    PassWord = MD5Helper.MD5Encrypt64("123456"),
                    PhoneNum = "13333333333",
                    Email = "13333333333@qq.com",
                    CreateTime = DateTime.Now,
                    //DepartmentId = rsbDepartment.Id
                };

                var zhangfei = new UserInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    Account = "zhangfei",
                    UserName = "张飞",
                    Sex = 1,
                    PassWord = MD5Helper.MD5Encrypt64("123456"),
                    PhoneNum = "15555555555",
                    Email = "15555555555@qq.com",
                    CreateTime = DateTime.Now
                };

                var guanyu = new UserInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    Account = "guanyu",
                    UserName = "关羽",
                    Sex = 1,
                    PassWord = MD5Helper.MD5Encrypt64("123456"),
                    PhoneNum = "16666666666",
                    Email = "16666666666@qq.com",
                    CreateTime = DateTime.Now
                };

                context.UserInfo.AddRange(admin, liubei, zhangfei, guanyu);
                #endregion

                #region 初始化角色相关信息
                var adminRole = new RoleInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    RoleName = "系统管理员",
                    Description = "系统管理员"
                };
                var bmldRole = new RoleInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    RoleName = "部门领导",
                    Description = "部门领导"
                };
                var ptygRole = new RoleInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    RoleName = "普通员工",
                    Description = "普通员工"
                };
                context.RoleInfo.AddRange(adminRole, bmldRole, ptygRole);

                //添加管理员为系统管理员角色
                context.UserInfoRoleInfo.AddRange(new UserInfoRoleInfo() //系统管理员角色
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    RoleId = adminRole.Id,
                    UserId = admin.Id
                }, new UserInfoRoleInfo() //普通员工角色
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    RoleId = ptygRole.Id,
                    UserId = zhangfei.Id
                }, new UserInfoRoleInfo() //部门领导角色
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    RoleId = bmldRole.Id,
                    UserId = liubei.Id
                });
                #endregion

                #region  初始化菜单相关信息
                var parentSys = new MenuInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    Title = "系统管理",
                    Icon = "fa fa-window-maximize",
                    Target = "_self",
                    Level = 1,
                    Sort = 10000
                };

                var profession = new MenuInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    Title = "业务管理",
                    Icon = "fa fa-window-maximize",
                    Target = "_self",
                    Level = 1,
                    Sort = 20000
                };

                context.MenuInfo.AddRange(parentSys, profession, new MenuInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    Title = "用户管理",
                    Href = "/UserInfo/UserInfoListView",
                    Icon = "fa fa-window-maximize",
                    Target = "_self",
                    Level = 2,
                    Sort = 11000,
                    ParentId = parentSys.Id
                }, new MenuInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    Title = "角色管理",
                    Href = "/RoleInfo/RoleInfoListView",
                    Icon = "fa fa-window-maximize",
                    Target = "_self",
                    Level = 2,
                    Sort = 12000,
                    ParentId = parentSys.Id
                }, new MenuInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    Title = "菜单管理",
                    Href = "/MenuInfo/MenuInfoListView",
                    Icon = "fa fa-window-maximize",
                    Target = "_self",
                    Level = 2,
                    Sort = 13000,
                    ParentId = parentSys.Id
                }, new MenuInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    Title = "通知管理",
                    Href = "/Notice/NoticeListView",
                    Icon = "fa fa-window-maximize",
                    Target = "_self",
                    Level = 2,
                    Sort = 14000,
                    ParentId = parentSys.Id
                }, new MenuInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    Title = "工程分类管理",
                    Href = "/Classification/ClassificationListView",
                    Icon = "fa fa-window-maximize",
                    Target = "_self",
                    Level = 2,
                    Sort = 21000,
                    ParentId = profession.Id
                }, new MenuInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    Title = "材料管理",
                    Href = "/Material/MaterialListView",
                    Icon = "fa fa-window-maximize",
                    Target = "_self",
                    Level = 2,
                    Sort = 22000,
                    ParentId = profession.Id
                }, new MenuInfo()
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    Title = "订单管理",
                    Href = "/Order/OrderListView",
                    Icon = "fa fa-window-maximize",
                    Target = "_self",
                    Level = 2,
                    Sort = 23000,
                    ParentId = profession.Id
                });



                #endregion

                #region 初始化通知相关信息
                context.Notice.AddRange(new Notice()
                {
                    Id = Guid.NewGuid().ToString(),
                    Title = "2024春节放假通知",
                    CreateTime = DateTime.Now,
                    CreatorId = admin.Id,
                    Content = "2024年春节放假时间为2024-02-08至2024-02-18。\r\n祝贺大家春节快乐。 \r\n\r\n某某家装公司人事部"
                });

                context.Notice.AddRange(new Notice()
                {
                    Id = Guid.NewGuid().ToString(),
                    Title = "2024春节放假通知",
                    CreateTime = DateTime.Now,
                    CreatorId = admin.Id,
                    Content = "各位同事，2024年工作开始动工，请安排好自己手上的工作。\r\n\r\n某某家装公司人事部"
                });
                #endregion

                #region 初始化工程分类相关信息
                var cggc = new Classification()
                {
                    Id = Guid.NewGuid().ToString(),
                    Title = "橱柜工程",
                    CreateTime = DateTime.Now,
                    Remark = "① 橱柜单价不含五金件，其费用另计。"
                };

                var mcgc = new Classification()
                {
                    Id = Guid.NewGuid().ToString(),
                    Title = "门、窗、门窗套、窗帘套工程",
                    CreateTime = DateTime.Now,
                    Remark = "①楼梯下天棚，墙面包角等另套相应定额：   \r\n②扶手大弯头（U字型）主材价乘2.5，其他辅材、机械、人工及损耗乘2计算"
                };

                var jjgc = new Classification()
                {
                    Id = Guid.NewGuid().ToString(),
                    Title = "家具工程",
                    CreateTime = DateTime.Now,
                    Remark = "①饰面板及线条用材品种不同时，按实调整补差价，定制木线按实补差价。\r\n②书柜、壁柜、装饰柜，酒柜等项目备注中有不含搁板(门)玻璃说明的项目,其搁板均为玻璃搁板,如实际需采用木搁板时,需套用(柜内饰面搁板增加)项目补差价。\r\n③小五金、挂衣杆、玻璃等定额均不包括在内(分别套用小五金和玻璃安装工程)\r\n④实际工艺和项目与备注说明有出入时，按工艺增加项目中的相应定额单价进行调整，找补差价"
                };

                context.Classification.AddRange(cggc, mcgc, jjgc);

                #endregion

                #region 初始化材料相关信息

                context.Material.AddRange(new Material()
                {
                    Id = Guid.NewGuid().ToString(),
                    Title = "厨房低柜（防火板，现场制作）",
                    Unit = "米",
                    Price = 842.84M,
                    PriceMain = 456.78M,
                    PriceAssistant = 80.86M,
                    PriceMachine = 4.20M,
                    PriceArtificial = 283.50M,
                    PriceLoss = 17.50M,
                    ClassificationId = cggc.Id,
                    CreateTime = DateTime.Now,
                    Remark = "饰面板饰面，木工板立架，单面木格子，木线条封边，不含玻璃"
                }, new Material()
                {
                    Id = Guid.NewGuid().ToString(),
                    Title = "厨房吊柜（防火板，现场制作）",
                    Unit = "米",
                    Price = 601.14M,
                    PriceMain = 289.29M,
                    PriceAssistant = 77.05M,
                    PriceMachine = 3.00M,
                    PriceArtificial = 220.50M,
                    PriceLoss = 11.30M,
                    ClassificationId = cggc.Id,
                    CreateTime = DateTime.Now,
                    Remark = "木工板立架，防火板（80/张）内外饰面PVC条封面，含门制安"
                }, new Material()
                {
                    Id = Guid.NewGuid().ToString(),
                    Title = "圆弧双面凹凸造型门",
                    Unit = "扇",
                    Price = 1189.35M,
                    PriceMain = 634.71M,
                    PriceAssistant = 48.64M,
                    PriceMachine = 7.00M,
                    PriceArtificial = 472.50M,
                    PriceLoss = 26.50M,
                    ClassificationId = mcgc.Id,
                    CreateTime = DateTime.Now,
                    Remark = "饰面板饰面，木工板立架，九厘板基层,凹凸双面，木线条封边"
                }, new Material()
                {
                    Id = Guid.NewGuid().ToString(),
                    Title = "单面木格子全玻门",
                    Unit = "扇",
                    Price = 559.87M,
                    PriceMain = 290.19M,
                    PriceAssistant = 16.28M,
                    PriceMachine = 4.00M,
                    PriceArtificial = 237.30M,
                    PriceLoss = 12.10M,
                    ClassificationId = mcgc.Id,
                    CreateTime = DateTime.Now,
                    Remark = "饰面板饰面，木工板立架，单面木格子，木线条封边，不含玻璃"
                });

                #endregion

                #region 初始化订单
                context.Order.Add(new Order
                {
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    CustomerName = "曹操",
                    CustomerPhone = "13333333333",
                    CustomerAddress = "南宁市创新路23号-8栋C座-201号",
                    AgentName = "荀彧",
                    AgentPhone = "12222222222",
                    Area = 111.11,
                    CreatorId = liubei.Id,
                    DesignerId = guanyu.Id,
                    StartTime = new DateTime(2024, 1, 1),
                    EndTime = new DateTime(2024, 3, 1),
                    Title = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}-曹操-南宁市创新路23号-8栋C座-201号"
                });
                #endregion  

                context.SaveChanges();
            }


        }
    }
}
